#min required version of cmake for compatibility
cmake_minimum_required(VERSION 3.15)
#declaration of project and languages in use
project(faust2clap LANGUAGES C CXX)

#specify c++17 and macos deployment target
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.11" CACHE STRING "macOS target")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

#avoid installing clap sdk during build
set(CLAP_NO_INSTALL ON CACHE BOOL "" FORCE)

#enable post-build copying of plugin to standard location
set(COPY_AFTER_BUILD ON CACHE BOOL "Copy the CLAP plugin to system folder after build")

#include required external libraries
add_subdirectory(${CMAKE_SOURCE_DIR}/../../external/clap-sdk clap-sdk)
add_subdirectory(${CMAKE_SOURCE_DIR}/../../external/clap-helpers clap-helpers)

#build a static lib to hold faust GUI glue (global symbols)
add_library(faust_gui_glue STATIC lib/faust_gui_glue.cpp)

#make sure it sees FAUST GUI headers (like faust/gui/GUI.h)
target_include_directories(faust_gui_glue PRIVATE
  ${CMAKE_SOURCE_DIR}/../../architecture
  ${CMAKE_SOURCE_DIR}/architecture/faust/gui
)

#hardcode RtMidi paths for Homebrew on Apple Silicon
set(RTMIDI_LIBRARY /opt/homebrew/Cellar/rtmidi/6.0.0/lib/librtmidi.dylib CACHE FILEPATH "Hardcoded RtMidi lib path")
set(RTMIDI_INCLUDE_DIR /opt/homebrew/Cellar/rtmidi/6.0.0/include CACHE PATH "Hardcoded RtMidi include path")
message(STATUS "Final RtMidi include: ${RTMIDI_INCLUDE_DIR}")


message(STATUS "Using hardcoded RtMidi:")
message(STATUS "  Library: ${RTMIDI_LIBRARY}")
message(STATUS "  Include: ${RTMIDI_INCLUDE_DIR}")


#search for all *_clap.cpp sources generated by Faust
file(GLOB_RECURSE FAUST_SOURCES "${CMAKE_SOURCE_DIR}/../../build/*_clap.cpp")

#loop through each generated file and add a CLAP plugin target
foreach(FAUST_CPP ${FAUST_SOURCES})
  get_filename_component(FILENAME ${FAUST_CPP} NAME_WE) # e.g. freeverb_clap
  string(REPLACE "_clap" "" PLUGIN_NAME ${FILENAME})     # strip _clap

  #declare the plugin module
  add_library(${PLUGIN_NAME} MODULE ${FAUST_CPP})

  #add plugin.cc to get full implementation of clap::helpers::Plugin<>
  target_sources(${PLUGIN_NAME}
    PRIVATE
      ${CMAKE_SOURCE_DIR}/lib/plugin.cc
  )

  #include necessary directories for compilation
  target_include_directories(${PLUGIN_NAME} PRIVATE
    ${CMAKE_SOURCE_DIR}/../../build
    ${CMAKE_SOURCE_DIR}/../../architecture
    ${CMAKE_SOURCE_DIR}/architecture/faust/gui
    ${CMAKE_SOURCE_DIR}/architecture/faust/midi
    ${CMAKE_SOURCE_DIR}/../../architecture/faust/dsp
    ${CMAKE_SOURCE_DIR}/../../external/clap-helpers/include
    ${CMAKE_SOURCE_DIR}/../../external/clap-sdk/include
    ${RTMIDI_INCLUDE_DIR} # RtMidi include path
    ${CMAKE_SOURCE_DIR}/../../build/${PLUGIN_NAME}
  )

  #link against the clap core library and clap helpers and GUI glue
  target_link_libraries(${PLUGIN_NAME} PRIVATE
    clap
    clap-helpers
    faust_gui_glue
    ${RTMIDI_LIBRARY} # RtMidi dynamic library
  )

  #use one shared plist template for all
  configure_file(
    ${CMAKE_SOURCE_DIR}/cmake/generic.plist.in
    ${CMAKE_CURRENT_BINARY_DIR}/${PLUGIN_NAME}.plist @ONLY
  )

  #set bundle properties for macOS
  set_target_properties(${PLUGIN_NAME} PROPERTIES
    OUTPUT_NAME ${PLUGIN_NAME}
    BUNDLE TRUE
    BUNDLE_EXTENSION clap
    MACOSX_BUNDLE_GUI_IDENTIFIER org.faust.${PLUGIN_NAME}
    MACOSX_BUNDLE_BUNDLE_NAME ${PLUGIN_NAME}
    MACOSX_BUNDLE_BUNDLE_VERSION "1.0"
    MACOSX_BUNDLE_SHORT_VERSION_STRING "1.0"
    MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_BINARY_DIR}/${PLUGIN_NAME}.plist
  )

  #copy plugin to user's system folder after build
  if (APPLE AND COPY_AFTER_BUILD)
    message(STATUS "Will copy plugin '${PLUGIN_NAME}' after build")
    set(products_folder ${CMAKE_BINARY_DIR})
    add_custom_command(TARGET ${PLUGIN_NAME} POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E echo "Installing ${products_folder}/${PLUGIN_NAME}.clap to ~/Library/Audio/Plug-Ins/CLAP/"
      COMMAND ${CMAKE_COMMAND} -E make_directory "$ENV{HOME}/Library/Audio/Plug-Ins/CLAP"
      COMMAND ${CMAKE_COMMAND} -E rm -rf "$ENV{HOME}/Library/Audio/Plug-Ins/CLAP/${PLUGIN_NAME}.clap"
      COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${products_folder}/${PLUGIN_NAME}.clap"
        "$ENV{HOME}/Library/Audio/Plug-Ins/CLAP/${PLUGIN_NAME}.clap"
    )
  endif()
endforeach()
